import {Request, Response, NextFunction, Router} from "express";
import {Repository} from "sequelize-typescript";
import {check, validationResult, body} from 'express-validator'
import {Like} from "../models";
import {checkToken} from "../middlewares/jwt";

interface BodyRequestBody extends Request {
    body: {
        [key: string]: string | undefined
    }
}

const likeRouterFactory = (
    likeRepository: Repository<Like>,
    ) => Router()
        .post('/add', checkToken, [
                body('dy_id').isLength({min: 1}),
                body('user_id').isLength({min: 1}),
            ], async (req: BodyRequestBody, res: Response, next: NextFunction) => {
                const error = validationResult(req)
                if (!error.isEmpty()) {
                    return res.status(422).json({
                        errCode: 10000,
                        msg: error.array()
                    })
                }
                let dy_id = req.body.dy_id, user_id = req.body.user_id
                try {
                    if (dy_id && user_id) {
                        const list = await likeRepository.findOne({
                            where: {
                                dy_id: dy_id,
                                user_id: user_id
                            }
                        })
                        if (!list) {
                            console.log('--- 点赞 ---');
                            await likeRepository.create({
                                dy_id: dy_id,
                                user_id: user_id
                            })
                            return res.json({
                                code: 1,
                                msg: '点赞成功'
                            })
                        }
                        return res.json({
                            code: 0,
                            msg: '已经点赞过了'
                        })
                    }
                    return res.json({
                        errCode: 10001,
                        msg: '操作失败'
                    })
                } catch (e) {
                    return res.json({
                        errCode: 10001,
                        msg: '操作失败'
                    })
                    console.log(e);
                    next()
                }
            }
        )
        .post('/cancel', checkToken, [
                body('dy_id').isLength({min: 1}),
                body('user_id').isLength({min: 1}),
            ], async (req: BodyRequestBody, res: Response, next: NextFunction) => {
                const error = validationResult(req)
                if (!error.isEmpty()) {
                    return res.status(422).json({
                        errCode: 10000,
                        msg: error.array()
                    })
                }
                console.log('取消')
                let dy_id = req.body.dy_id, user_id = req.body.user_id
                try {
                    if (dy_id && user_id) {
                        const list = await likeRepository.findOne({
                            where: {
                                dy_id: dy_id,
                                user_id: user_id
                            }
                        })
                        if (list) {
                            console.log('--- 取消点赞 ---');
                            await likeRepository.destroy({
                                where: {
                                    dy_id: dy_id,
                                    user_id: user_id
                                }
                            })
                            return res.json({
                                code: 1,
                                msg: '取消点赞成功'
                            })
                        }
                    }
                    return res.json({
                        errCode: 10001,
                        msg: '操作失败'
                    })
                } catch (e) {
                    console.log(e);
                    next()
                }
            }
            // homeRepository.findAll()
            // .then(posts => res.json(posts))
            // .catch(next)
        )
    // .get('/', async (req: BodyRequestBody, res: Response, next: NextFunction) => {
    //         const list = await homeRepository.findAll()
    //         res.json(list || {
    //             errCode: 10001,
    //             msg: '数据获取失败'
    //         })
    //     }
    //     // homeRepository.findAll()
    //     // .then(posts => res.json(posts))
    //     // .catch(next)
    // )

    // .get('/', async (req, res, next) => {
    //         const list = await homeRepository.findAll()
    //         res.json(list || {
    //             errCode: 10001,
    //             msg: '数据获取失败'
    //         })
    //     }
    //     // homeRepository.findByPk(req.params.id)
    //     // .then(post => post
    //     //   ? res.json(post)
    //     //   : next({statusCode: 404}))
    //     // .catch(next)
    // )
    //
    // .post('/posts', (req, res, next) =>
    //     homeRepository.create(req.body)
    //         .then(post => res.json(post))
    //         .catch(next)
    // )

;
export default likeRouterFactory
